//
//  XLUserInfo.m
//  XLBaseModule
//
//  Created by 于鹏 on 2017/8/8.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//
//------本地信息存储-------

#import "XLUserInfo.h"
#import <objc/runtime.h>

#pragma mark - 给object赋值
@implementation NSObject (XL)
- (void)setObject:(NSString *)object forKey:(NSString *)key {
    object = ([object isKindOfClass:[NSNull class]] || object == nil) ? @"" : object;
    [[NSUserDefaults standardUserDefaults]setObject:object forKey:key];
}

- (NSString *)getObjectForKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults]objectForKey:key];
}
@end

#pragma mark - 用户管理
@implementation XLUserInfo

+ (XLUserInfo *)shareUser {
    static XLUserInfo *userInfo;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userInfo = [[XLUserInfo alloc]init];
    });
    return userInfo;
}

//登出清除数据
- (void)loginOut {
    NSArray *allAttribute = [self getAllProperties];
    for (NSString *attribute in allAttribute) {
        NSLog(@"attribute:%@",attribute);
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:attribute];
    }
    [self synchronize];
}

//同步保存
- (void)synchronize {
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma 赋值并保存

- (void)setUserID:(NSString *)userID {
    [self setObject:userID forKey:@"userID"];
}

- (void)setUserName:(NSString *)userName {
    [self setObject:userName forKey:@"userName"];
}

- (void)setNickName:(NSString *)nickName {
    [self setObject:nickName forKey:@"nickName"];
}

- (void)setUserLogo:(NSString *)userLogo {
    [self setObject:userLogo forKey:@"userLogo"];
}

- (void)setPhone:(NSString *)phone {
    [self setObject:phone forKey:@"phone"];
}

- (void)setGender:(NSString *)gender {
    [self setObject:gender forKey:@"gender"];
}

- (void)setHospital:(NSString *)hospital {
    [self setObject:hospital forKey:@"hospital"];
}

- (void)setDoctorDepartment:(NSString *)doctorDepartment {
    [self setObject:doctorDepartment forKey:@"doctorDepartment"];
}

- (void)setRYToken:(NSString *)RYToken {
    [self setObject:RYToken forKey:@"RYToken"];
}

- (void)setRYID:(NSString *)RYID {
    [self setObject:RYID forKey:@"RYID"];
}

- (void)setRYName:(NSString *)RYName {
    [self setObject:RYName forKey:@"RYName"];
}

- (void)setRYLogo:(NSString *)RYLogo {
    [self setObject:RYLogo forKey:@"RYLogo"];
}
- (void)setTokenCode:(NSString *)tokenCode {
    [self setObject:tokenCode forKey:@"tokenCode"];
}

- (void)setIsLogin:(BOOL )isLogin {
    [[NSUserDefaults standardUserDefaults]setBool:isLogin forKey:@"isLogin"];
}

- (void)setDisableMessageAlertSound:(BOOL)disableMessageAlertSound {
    [[NSUserDefaults standardUserDefaults]setBool:disableMessageAlertSound forKey:@"disableMessageAlertSound"];
}

- (void)setEnteredID:(NSString *)enteredID {
    [self setObject:enteredID forKey:@"enteredID"];
}

#pragma mark - 取值

- (NSString *)userID {
    return [self getObjectForKey:@"userID"];
}

- (NSString *)userName {
    return [self getObjectForKey:@"userName"];
}

- (NSString *)nickName {
    return [self getObjectForKey:@"nickName"];
}

- (NSString *)userLogo {
    return [self getObjectForKey:@"userLogo"];
}

- (NSString *)phone {
    return [self getObjectForKey:@"phone"];
}

- (NSString *)gender {
    return [self getObjectForKey:@"gender"];
}

- (NSString *)hospital {
    return [self getObjectForKey:@"hospital"];
}

- (NSString *)doctorDepartment {
    return [self getObjectForKey:@"doctorDepartment"];
}

- (NSString *)RYToken {
    return [self getObjectForKey:@"RYToken"];
}

- (NSString *)RYID {
    return [self getObjectForKey:@"RYID"];
}

- (NSString *)RYName {
    return [self getObjectForKey:@"RYName"];
}

- (NSString *)RYLogo {
    return [self getObjectForKey:@"RYLogo"];
}

- (NSString *)tokenCode {
    return [self getObjectForKey:@"tokenCode"];
}

- (BOOL)isLogin {
    return [[NSUserDefaults standardUserDefaults]boolForKey:@"isLogin"];
}

- (BOOL)disableMessageAlertSound {
    return [[NSUserDefaults standardUserDefaults]boolForKey:@"disableMessageAlertSound"];
}

- (NSString *)enteredID {
    return [self getObjectForKey:@"enteredID"];
}

- (XLServiceInfo *)serviceInfo {
    if (!_serviceInfo) {
        _serviceInfo = [[XLServiceInfo alloc]init];
    }
    return _serviceInfo;
//    return [[XLServiceInfo alloc]init];
}

#pragma mark - Privety Method
//获取所有属性
- (NSArray *)getAllProperties {
    u_int count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count; i++) {
        const char *propertyName = property_getName(properties[i]);
        [propertiesArray addObject:[NSString stringWithUTF8String:propertyName]];
    }
    free(properties);
    return propertiesArray;
}

//- (void)setObject:(NSString *)object forKey:(NSString *)key {
//    object = ([object isKindOfClass:[NSNull class]] || object == nil) ? @"" : object;
//    [[NSUserDefaults standardUserDefaults]setObject:object forKey:key];
//}
//
//- (NSString *)getObjectForKey:(NSString *)key {
//    return [[NSUserDefaults standardUserDefaults]objectForKey:key];
//}

@end


#pragma mark - 服务器配置
@implementation XLServiceInfo

//+ (XLServiceInfo *)shareServiceInfo {
//    static XLServiceInfo *serviceInfo;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        serviceInfo = [[XLServiceInfo alloc]init];
//    });
//    return serviceInfo;
//}

- (instancetype)initWithServiceAddress:(NSString *)serviceAddress port:(NSString *)port tcpPort:(NSString *)tcpPort {
    if (self = [super init]) {
        self.serviceAddress = serviceAddress;
        self.port = port;
        self.tcpPort = tcpPort;
    }
    return self;
}


- (void)setServiceAddress:(NSString *)serviceAddress {
    [self setObject:serviceAddress forKey:@"serviceAddress"];
}

- (void)setPort:(NSString *)port {
    [self setObject:port forKey:@"port"];
}

- (void)setTcpPort:(NSString *)tcpPort {
    [self setObject:tcpPort forKey:@"tcpPort"];
}

- (NSString *)serviceAddress {
    return [self getObjectForKey:@"serviceAddress"];
}

- (NSString *)port {
    return [self getObjectForKey:@"port"];
}

- (NSString *)tcpPort {
    return [self getObjectForKey:@"tcpPort"];
}

- (BOOL)isVaild {
    return (self.serviceAddress.length > 0 && self.port.length > 0);
}

- (NSString *)baseURL {
    NSString *baseURL = [NSString stringWithFormat:@"%@:%@",self.serviceAddress,self.port];
    return [self addHTTPWithUrl:baseURL];
}

- (NSString *)addHTTPWithUrl:(NSString *)url {
    NSString *http = @"http://";
    NSString *urlString = url.lowercaseString;
    if ([urlString containsString:http]) {
        return urlString;
    } else {
        return [NSString stringWithFormat:@"%@%@",http,urlString];
    }
}

@end

